﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Configuration;
using System.Web.Profile;
using Huayou.UI.WebControls;
using System.Data;
using HySoft.Cash.BaseClass;
using System.Data.SqlClient;
using Huayou.Common;
using HySoft.Cash;
namespace PayRoller.Manage.OrgInfo
{
    public class DailyPayment : ParentPage, IRequiresSessionState
    {
        // Fields
        protected Huayou.UI.WebControls.DataGridTable dgdList;
        protected DropDownList dlOrgList;
        public string EmpSid = "";
        protected HtmlForm form1;
        protected HtmlHead Head1;
        protected Label lblTip;
        public int OrgSid = 0;
        protected TextBox txtParamstr;

        // Methods
        protected void dgdList_DataBinding(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            this.OrgSid = Convert.ToInt32(this.Request.QueryString["orgID"]);
            if (this.OrgSid <= 0)
            {
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@IsOnlyDisplayUnpaid", 1), new SqlParameter("@DateCalced",SafeDateTime.LocalToday) };
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.StoredProcedure, "GetDailyPayment", commandParameters).Tables[0];
            }
            else
            {
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@OrgSid", this.OrgSid), new SqlParameter("@IsOnlyDisplayUnpaid", 1), new SqlParameter("@DateCalced", SafeDateTime.LocalToday) };
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.StoredProcedure, "GetDailyPayment", commandParameters).Tables[0];
            }

            CommonBasePage.SetPage(this.dgdList, dt);
            base.AddValue("pageno", Convert.ToString((int)(this.dgdList.CurrentPageIndex + 1)));
            this.txtParamstr.Text = base.PackPart(base.ParamValue);
        }

        protected void dgdList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgdList.CurrentPageIndex = e.NewPageIndex;
            this.dgdList.DataBind();
        }

        protected void dlOrgList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.dgdList.DataBind();
        }

        protected void dgdList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType listItemType = e.Item.ItemType;
            if (listItemType == ListItemType.Item || listItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = e.Item.DataItem as DataRowView;
                if (drv != null)
                {
                    string currentEmpID = drv["EmpSid"].ToString();
                    string currentEmpName = drv["EmpName"].ToString();
                    string currentEmpCode = drv["OrgEmpSid"].ToString();
                    string currentOrgID = drv["OrgSid"].ToString();
                    int currentOrgIDInt = Convert.ToInt32(currentOrgID);
                    string currentOrgName = drv["OrgName"].ToString();
                    string currentPaymentAccount = drv["AccountNum"].ToString();
                    //string currentOrgAccount = drv["UserName"].ToString();AccountNum
                    int currentPaymentID = Convert.ToInt32(drv["Sid"]);
                    PayManualStatuses currentPayStatus = (PayManualStatuses)Convert.ToInt32(drv["other1"]);

                    Literal litEmpSid = e.Item.FindControl("litEmpSid") as Literal;
                    if (litEmpSid != null)
                    {
                        string empURL = this.Page.ResolveClientUrl("~/Manage/OrgInfo/EmpDetail.aspx");
                        litEmpSid.Text = string.Format("<a  href='{0}?OrgSid={1}&Sid={2}'>{3}</a>", empURL, currentOrgID, currentEmpID,currentEmpCode);
                    }

                    Literal litEmpName = e.Item.FindControl("litEmpName") as Literal;
                    if (litEmpName != null)
                    {
                        string empURL = this.Page.ResolveClientUrl("~/Manage/OrgInfo/EmpDetail.aspx");
                        litEmpName.Text = string.Format("<a  href='{0}?OrgSid={1}&Sid={2}'>{3}</a>", empURL, currentOrgID, currentEmpID, currentEmpName);
                    }

                    Literal litUserName = e.Item.FindControl("litUserName") as Literal;
                    if (litUserName != null)
                    {
                        litUserName.Text = string.Format("{0}", currentPaymentAccount);
                        //string empURL = this.Page.ResolveClientUrl("~/Manage/OrgInfo/Index.aspx");
                        //litUserName.Text = string.Format("<a  href='{0}?OrgSid={1}'>{2}</a>", empURL, currentOrgID, currentPaymentAccount);
                    }

                    Literal litOrgName = e.Item.FindControl("litOrgName") as Literal;
                    if (litOrgName != null)
                    {
                        string empURL = this.Page.ResolveClientUrl("~/Manage/OrgInfo/Index.aspx");
                        litOrgName.Text = string.Format("<a  href='{0}?OrgSid={1}'>{2}</a>", empURL, currentOrgID, currentOrgName);
                    }


                    LinkButton libPayStatus = e.Item.FindControl("libPayStatus") as LinkButton;
                    Literal litPayStatus = e.Item.FindControl("litPayStatus") as Literal;
                    if (currentPayStatus == PayManualStatuses.NeedPay)
                    {
                        if (libPayStatus != null)
                        {
                            libPayStatus.CommandArgument = currentPaymentID.ToString();
                            litPayStatus.Visible = false;
                        }
                    }
                    else
                    {
                        string payStatusDisplay = string.Empty;
                        if (litPayStatus != null)
                        {
                            payStatusDisplay = "Paid";
                            litPayStatus.Text = payStatusDisplay;
                            libPayStatus.Visible = false;
                        }
                    }

                    Literal litInvoiceStatus = e.Item.FindControl("litInvoiceStatus") as Literal;
                    if (litInvoiceStatus != null)
                    {
                        bool isOverDued = false;
                        for (int i = 0; i < OverDuedInvoiceList.Count; i++)
                        {
                            if (OverDuedInvoiceList[i].OrgSid == currentOrgIDInt)
                            {
                                isOverDued = true;
                                break;
                            }
                        }

                        if (isOverDued == true)
                        {
                            litInvoiceStatus.Text = "OverDue";
                        }
                        else
                        {
                            litInvoiceStatus.Text = "Normal";
                        }
                    }
                }
            }
        }

        private List<TinvoicelistDT> overDuedInvoiceList;

        private List<TinvoicelistDT> OverDuedInvoiceList
        {
            get
            {
                if (overDuedInvoiceList == null)
                {
                    overDuedInvoiceList = TinvoicelistBN.GetOverDueInvoiceList();
                }
                return overDuedInvoiceList;
            }
        }

        void libPayStatus_Click(object sender, EventArgs e)
        {
            int paymentID = 0;
            LinkButton t = sender as LinkButton;
            if (t != null)
            {
                int.TryParse(t.CommandArgument, out paymentID);
            }

            if (paymentID > 0)
            {
                String connString = AppSettings.ConnectionString;
                string commString = string.Format("UPDATE [tEmpPayment]   SET [other1] = {0} WHERE [Sid]={1}", (int)PayManualStatuses.Paid, paymentID);
                SqlHelper.ExecuteNonQuery(connString, CommandType.Text, commString);

                this.dgdList.DataBind();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                //this.MakeInvoice();
                //this.GetOrgList();
                base.ParamValue = base.UnPack();
                this.txtParamstr.Text = base.PackPart(base.ParamValue);
                string str = base.GetValue("pageno");
                if (str != null)
                {
                    this.dgdList.CurrentPageIndex = Convert.ToInt32(str) - 1;
                }
                this.dgdList.DataBind();
            }
            else
            {
                base.ParamValue = base.UnPackPart(this.txtParamstr.Text);
            }
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

        protected void dgdList_ItemCreated(object sender, DataGridItemEventArgs e)
        {
            ListItemType listItemType = e.Item.ItemType;
            if (listItemType == ListItemType.Item || listItemType == ListItemType.AlternatingItem)
            {
                LinkButton libPayStatus = e.Item.FindControl("libPayStatus") as LinkButton;
                if (libPayStatus != null)
                {
                    libPayStatus.Click += new EventHandler(libPayStatus_Click);
                }

            }
        }
    }

}
